package com.ipet.app.adapter.ViewHolder;

import android.content.Intent;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ipet.app.R;
import com.ipet.app.adapter.ViewMulImageAdapter;
import com.ipet.app.bean.HotPost;
import com.ipet.app.bean.ReplyInfo;
import com.ipet.app.ui.common.PhotoViewActivity;
import com.ipet.app.util.ImageUtil;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

/**
 * Created by jindongping on 16/3/18.
 */
public class ReplyPostViewHolder extends BaseViewHolder<ReplyInfo> {
    TextView name, time, title, content;
    RecyclerView imageRecyclerView;
    HotPost hotPost;

    public ReplyPostViewHolder(ViewGroup parent, @LayoutRes int res, HotPost hotPost) {
        super(parent, res);
        name = $(R.id.post_name);
        time = $(R.id.post_time);
        title = $(R.id.post_title);
        content = $(R.id.post_content);
        imageRecyclerView = $(R.id.post_image_recyclerView);
        this.hotPost = hotPost;
    }


    @Override
    public void setData(final ReplyInfo replyInfo) {
        super.setData(replyInfo);
        //初始化数据
        name.setText(hotPost.getNickname());
        time.setText(hotPost.getCreate_time());
        if (hotPost.getTheme() != null && hotPost.getTheme().length() > 1) {
            title.setVisibility(View.VISIBLE);
            title.setText(hotPost.getTheme());
        }
        content.setText(hotPost.getContent());

        //设置 多图显示
        if (hotPost.getPicurl() != null) {
            final String[] images = new Gson().fromJson(hotPost.getPicurl(), String[].class);
            ViewMulImageAdapter adapter = new ViewMulImageAdapter(getContext());
            adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    Intent intent = new Intent(getContext(), PhotoViewActivity.class);
                    intent.putExtra("images", images);
                    intent.putExtra("position", position);
                    getContext().startActivity(intent);
                }
            });
            adapter.addAll(ImageUtil.getInstance(getContext()).convertUrlToDisplayImg(images,0));
            if (images.length >= 3) {
                imageRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
            } else {
                imageRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), images.length));
            }
            imageRecyclerView.setAdapter(adapter);
        }
    }

}